/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.leocops.moviezona.servlet;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.PropertyResourceBundle;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/**
 *
 * @author Admin
 */
public class ServletListener implements ServletContextListener {

    public static ServletListener s_self;
    public static Object sync = new Object();    
    
    public static final String CONFIG_FILE = "CONFIG_FILE";
    public static final String SAMPLE_MESSAGE = "SAMPLE_MESSAGE";
    
    public static String configFile = null;    
    public static String sampleMessage = null;
    

    public ServletListener() {
        synchronized(sync) {
            if(null != s_self) throw new IllegalStateException("Exception");
            s_self = this;
        }
    }

    public static ServletListener getInstance() {
        synchronized(sync) {
            if (null == s_self) {
                throw new IllegalStateException("Exception");
            }
            return s_self;
        }
    }


    public void contextInitialized(ServletContextEvent sce) {

        configFile = (String) sce.getServletContext().getInitParameter(CONFIG_FILE);
        try {
            FileReader fileReader = new FileReader(sce.getServletContext().getRealPath(configFile));
            PropertyResourceBundle systemProperties = new PropertyResourceBundle(fileReader);            
            sampleMessage = systemProperties.getString(SAMPLE_MESSAGE);
            
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }

    }

    public void contextDestroyed(ServletContextEvent sce) {

    }

}
